<?php
$pw = "peachtao";
if (isset ( $_POST ['pw'] ) && $_POST ['pw'] == $pw) {
	
	echo "操作开始<br/>";
	$start = explode ( ' ', microtime () );
	
	// Define path to application directory
	defined ( 'APPLICATION_PATH' ) || define ( 'APPLICATION_PATH', realpath ( dirname ( __FILE__ ) . '/../application' ) );
	
	// Define application environment
	defined ( 'APPLICATION_ENV' ) || define ( 'APPLICATION_ENV', (getenv ( 'APPLICATION_ENV' ) ? getenv ( 'APPLICATION_ENV' ) : 'production') );
	// Ensure library/ is on include_path
	set_include_path ( implode ( PATH_SEPARATOR, array (
			realpath ( APPLICATION_PATH . '/../libs' ),
			get_include_path () 
	) ) );
	
	require_once 'Zend/Config/Ini.php';
	
	$config = new Zend_Config_Ini ( APPLICATION_PATH . '/configs/server.ini', APPLICATION_ENV );
	
	$dbParams = $config->resources->db->params;
	
	$conn = mysql_connect ( preg_replace ( '/;port=/', ':', $dbParams->host ), $dbParams->username, $dbParams->password );
	if (! $conn) {
		die ( 'Could not connect: ' . mysql_error () );
	}
	@mysql_select_db ( $dbParams->dbname, $conn ) or die ( '指定的库不存在' . mysql_error () );
	@mysql_query ( 'SET NAMES UTF8' ) or die ( '字符集设置错误' . mysql_error () );
	
	$query = "show tables";
	
	$arr = array ();
	$q = @mysql_query ( $query ) or die ( mysql_error () );
	while ( $result = mysql_fetch_row ( $q ) ) {
		$tableName = $result [0];
		$query1 = "desc $tableName";
		$q1 = @mysql_query ( $query1 ) or die ( mysql_error () );
		$columns = array ();
		$pri = "";
		while ( $result1 = mysql_fetch_assoc ( $q1 ) ) {
			if ($result1 ["Key"] == "PRI") {
				$pri = $result1 ["Field"];
			}
			$columns [] = $result1;
		}
		$arr [] = array (
				"table" => $tableName,
				"pri" => $pri 
		);
		// print_r($result);
		// print_r($columns);
	}
	
	// 释放记录资源集
	mysql_free_result ( $q );
	
	// 最后一步，关闭数据库
	mysql_close ( $conn );
	
	print_r ( $arr );
	
	foreach ( $arr as $v ) {
		$table = preg_replace ( "/^wp_/", "", $v ['table'] );
		
		$pri = $v ['pri'];
		
		$table = ucfirst ( $table );
		$class = preg_replace ( "/_([a-z]{1})/e", "strtoupper('\\1')", $table );
		$model = file_get_contents ( "./modelTemplate/Model.php" );
		$model = preg_replace ( "/MMMMMMM/", "{$class}", $model );
		$model = preg_replace ( "/PRI/", "{$pri}", $model );
		// $model = preg_replace("//","",$model);
		file_put_contents ( APPLICATION_PATH . "/models/" . ucfirst ( $class ) . ".php", $model );
		
		$table = strtolower ( $table );
		
		$dbTable = file_get_contents ( "./modelTemplate/DbTable.php" );
		$dbTable = preg_replace ( "/MMMMMMM/", ucfirst ( $class ), $dbTable );
		$dbTable = preg_replace ( "/TABLE/", "{$table}", $dbTable );
		$dbTable = preg_replace ( "/PRI/", "{$pri}", $dbTable );
		// $model = preg_replace("//","",$model);
		file_put_contents ( APPLICATION_PATH . "/models/DbTable/" . ucfirst ( $class ) . ".php", $dbTable );
		
		$service = file_get_contents ( "./modelTemplate/Service.php" );
		$service = preg_replace ( "/MMMMMMM/", ucfirst ( $class ), $service );
		$service = preg_replace ( "/PRI/", "{$pri}", $service );
		
		// $model = preg_replace("//","",$model);
		file_put_contents ( APPLICATION_PATH . "/modules/front/services/" . ucfirst ( $class ) . ".php", $service );
	}
	
	$end = explode ( ' ', microtime () );
	
	echo "耗时：" . ($end [1] - $start [1]) . "s";
} else {
	$msg = isset ( $_POST ['pw'] ) && $_POST ['pw'] != $pw ? "密码不正确" : "";
	?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<title>Action导入</title>
<script type="text/javascript">

</script>
<style type="text/css">
* {
	margin: 0;
	padding: 0;
	font-size: 12px;
	line-height: 25px;
}

#loginBox {
	width: 300px;
	border: 1px solid #ccc;
	background: #FFF;
}

h1 {
	height: 35px;
	line-height: 35px;
	background: #e8e8e8;
	font-size: 14px;
	border-bottom: 1px solid #ccc;
	text-indent: 5px;
}

input.name,input.code {
	width: 150px;
	height: 25px;
	border: 1px solid #ccc;
	margin: 0 0 0 0px;
	padding: 0 0 0 20px;
}

input.verifyCode {
	height: 25px;
	width: 75px;
	border: 1px solid #ccc;
}

input.name {
	background: url("../../../images/login-username.gif") no-repeat left
		center;
}

input.code {
	background: url("../../../images/login-password.gif") no-repeat left
		center;
}

input.loginBtn,input.registerBtn {
	width: 54px;
	height: 21px;
	border: none;
	margin: 5px;
}

input.loginBtn {
	background: url("../../../images/login-btn.gif") no-repeat;
}

html,body {
	/*background: url("../images/background-top.png") no-repeat scroll 0 0 #318DAC;*/
	border: 0px solid #333333;
	color: #333333;
	font: 12px/150% Arial, Helvetica, sans-serif, '宋体';
	margin: 0;
	padding: 0;
	font-family: Arial, Tahoma, Verdana, sans-serif;
	font-size: 12px;
}

.logo {
	background: url("../../../images/logo.png") no-repeat scroll left top
		transparent;
	height: 70px;
	left: 10px;
	position: absolute;
	top: 10px;
	width: 170px;
}

.header {
	height: 88px;
	margin-top: auto;
	padding: 0 10px;
	position: relative;
	background: url("../../../images/background-top.png") no-repeat 0 0
		#318DAC;
}
</style>
</head>
<body onload="document.getElementById('pw').focus();">

	<div class="header">
		<!-- 	
	<div class="logo" title="China Fullfill"></div>
-->
	</div>

	<div style="margin-top: 100px; text-align: center">
		<center>
			<form name="login" id="loginForm" method="post" action="">
				<div id="loginBox" style="text-align: left">
					<h1>初始化Services,Models，DbTable</h1>
					<div
						style="margin-top: 5px; margin-left: 5px; font-size: 13px; color: red; text-align: center;"
						id="msg">
						<span><?php echo $msg?></span>
					</div>
					<div style="margin-top: 5px; margin-left: 5px; font-size: 13px;">
						请输入密码： <input type="text" class="name" name="pw" id="pw"
							tabindex="1" />
					</div>

					<p align="center">
						<input type="submit" value="" class="loginBtn" />
					</p>
				</div>
			</form>
		</center>
	</div>
</body>
</html>
<?php }?>
